﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ESF.General.MVP;
using ESF.General;
using ESF.SSO.Core.Entities.BasicReferences;
using ESF.SSO.Core.Entities;
using ESF.General.Entity;
using ESF.SSO.Core.BusinessLogic;

#endregion 


namespace ESF.SSO.Presenter.Admin.User
{
	public class BrowseUsersPresenter : BaseBrowsePagedPresenter<ESF.SSO.Core.Entities.User, IBrowseUsersViewer>
	{
		#region Constructor
		
		public BrowseUsersPresenter (IBrowseUsersViewer pViewer) : base (pViewer) {}
		
		#endregion
		
		
		#region Funciones

		public override void Load()
		{
			_Viewer.EnabledList = GeneralAdmin.GetYESNOList ();

			_Viewer.UserGroupList = GeneralAdmin.GetBasicReferenceList (BasicReferenceTypes.UserGroup);

			LoadSteps();	
		}


		public void LoadSteps()
		{
			_Viewer.ExtraAppSteps = CtrlBasicReference.List(BasicReferenceTypes.Application).Where (a => !string.IsNullOrEmpty(a.Extra_II)).ToList ();
		}
		

		public override void Clear()
		{
			base.Clear ();
			
			_Viewer.UserGroup = null;

			_Viewer.IsEnabled = null;
			_Viewer.Results = null;
			
			_Viewer.UserAlias = string.Empty;
			_Viewer.UserEmail = string.Empty;
			_Viewer.UserFirstName = string.Empty;
			_Viewer.UserLastName = string.Empty;
			_Viewer.UserSecondName = string.Empty;
			_Viewer.UserSurName = string.Empty;
			_Viewer.UserEnterpriseName = string.Empty;
			_Viewer.UserPositionName = string.Empty;
			_Viewer.UserProfessionName = string.Empty;
			
			_Viewer.filExtraValue = string.Empty;

			_Viewer.UserIdentificationNumber = string.Empty;
			_Viewer.UserIdentificationTypeID = null;
		}
		
		
		protected override BrowseResults<ESF.SSO.Core.Entities.User> GetPagedResults(PaggingInfo pPaggingInfo, out ESF.SSO.Core.Entities.User pFilter)
		{
			pFilter = new ESF.SSO.Core.Entities.User ()
			{
				Alias = _Viewer.UserAlias,
				FirstName = _Viewer.UserFirstName,
				SecondName = _Viewer.UserSecondName,
				SurName = _Viewer.UserSurName,
				LastName = _Viewer.UserLastName,
				Email = _Viewer.UserEmail,
				IdentificationType = new ESF.General.Entity.BasicReference () { ID = _Viewer.UserIdentificationTypeID },
				IdentificationNumber = _Viewer.UserIdentificationNumber,
				Enterprise = new Core.Entities.Enterprise () { Name = _Viewer.UserEnterpriseName },
				Position = _Viewer.UserPositionName,
				Profession = _Viewer.UserProfessionName,
				IsEnabled = _Viewer.IsEnabled,
				filExtraValue = _Viewer.filExtraValue,
				UserGroups = new List<General.Entity.BasicReference> () { _Viewer.UserGroup }
			};

			return CtrlUser.Browse (pFilter, pPaggingInfo);
		}


		public override void Delete(List<ESF.SSO.Core.Entities.User> pSelectedItems)
		{
			CtrlUser.Delete (_Viewer.SelectedItems);
		}
		
		#endregion
	}
}
